<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="参与排行榜的盒子id" prop="goodId">
        <el-input
          v-model="queryParams.goodId"
          placeholder="请输入参与排行榜的盒子id"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="排行榜生效时间">
        <el-date-picker
          v-model="daterangeEffectiveTime"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="排行榜类型" prop="leaderType">
        <el-select
          v-model="queryParams.leaderType"
          placeholder="请选择排行榜类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.top_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="谁创建的" prop="whosName">
        <el-input
          v-model="queryParams.whosName"
          placeholder="请输入谁创建的"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="最后一次编辑时间">
        <el-date-picker
          v-model="daterangeUpdateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="排行榜名" prop="leaderboardName">
        <el-input
          v-model="queryParams.leaderboardName"
          placeholder="请输入排行榜名"
          clearable
          
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:leaderboard:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:leaderboard:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:leaderboard:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:leaderboard:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="leaderboardList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="80" />
      <!-- <el-table-column label="排行榜类型" align="center" prop="leaderType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.top_type"
            :value="scope.row.leaderType"
          />
        </template>
      </el-table-column> -->
      <el-table-column label="排行榜名" align="center" prop="leaderboardName" />
      <!-- 
      <el-table-column
        label="参与排行榜的盒子id"
        align="center"
        prop="goodId"
      /> -->
      <!-- <el-table-column
        label="排行榜生效时间"
        align="center"
        prop="effectiveTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.effectiveTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column> -->
      <el-table-column
        label="是否开启"
        align="center"
        prop="xgAll"
        sortable="custom"
      >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.ifOpen == 0">否</el-tag>
          <el-tag v-else>是</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="谁创建的" align="center" prop="whosName" /> -->
      <el-table-column
        label="开始时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            scope.row.startTime
          }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="结束时间"
        align="center"
        prop="updateTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            scope.row.endTime
          }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:leaderboard:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:leaderboard:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleXq(scope.row)"
            v-hasPermi="['system:leaderboard:settle']"
            >详情</el-button
          >

          <!-- 结算 -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSettle(scope.row)"
            v-hasPermi="['system:leaderboard:settle']"
            >结算</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

    <!-- 添加或修改排行榜对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="排行榜名称" prop="goodId">
          <el-input
            v-model="form.leaderboardName"
            placeholder="请输入排行榜名称"
          />
        </el-form-item>
        <el-form-item label="排行榜生效时间" prop="effectiveTime">
          <el-date-picker
            type="datetimerange"
            v-model="time"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            placeholder="请选择排行榜生效时间"
            @change="endDate"
          >
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="排行榜生效时间" prop="effectiveTime">
          <el-date-picker
            clearable
            v-model="form.endTime"
             type="datetimerange"
            value-format="yyyy-MM-dd"
            placeholder="请选择排行榜结束时间"
          >
          </el-date-picker>
        </el-form-item> -->

        <!-- <el-form-item label="谁创建的" prop="whosName">
          <el-input v-model="form.whosName" placeholder="请输入谁创建的" />
        </el-form-item> -->
        <el-form-item label="排行榜名" prop="leaderboardName">
          <el-switch
            v-model.number="form.ifOpen"
            active-color="#13ce66"
            inactive-color="#F4F4F5"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 结算排行榜对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="openSettle"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form_end" :rules="rules" label-width="80px">
        <el-form-item label="结算开始时间" prop="effectiveTime">
          <el-date-picker
            v-model="form_end.startTime"
            type="datetime"
            placeholder="请选择结算开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="排行榜生效时间" prop="effectiveTime">
          <el-date-picker
            v-model="form_end.endTime"
            type="datetime"
            placeholder="请选择结算结束时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="jie_end">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listLeaderboard,
  getLeaderboard,
  delLeaderboard,
  addLeaderboard,
  updateLeaderboard,
  settleLeaderboard,
} from "@/api/top/top_list.js";

export default {
  name: "Leaderboard",
  dicts: ["top_type"],
  data() {
    return {
      form_end: {
        type: "",
        startTime: "",
        endTime: "",
        isDisable: 1,
      },
      time: null,

      openSettle: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排行榜表格数据
      leaderboardList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 排行榜名时间范围
      daterangeEffectiveTime: [],
      // 排行榜名时间范围
      daterangeCreateTime: [],
      // 排行榜名时间范围
      daterangeUpdateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodId: null,
        effectiveTime: null,
        leaderType: null,
        whosName: null,
        createTime: null,
        updateTime: null,
        leaderboardName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        startTime: [
          {
            required: true,
            message: "排行榜生效时间不能为空",
            trigger: "blur",
          },
        ],
        endTime: [
          {
            required: true,
            message: "排行榜结束时间不能为空",
            trigger: "change",
          },
        ],

        leaderboardName: [
          { required: true, message: "排行榜名不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    console.log(this.$store.getters);
    this.getList();
  },
  methods: {
    /** 查询排行榜列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (
        null != this.daterangeEffectiveTime &&
        "" != this.daterangeEffectiveTime
      ) {
        this.queryParams.params["beginEffectiveTime"] =
          this.daterangeEffectiveTime[0];
        this.queryParams.params["endEffectiveTime"] =
          this.daterangeEffectiveTime[1];
      }
      if (null != this.daterangeCreateTime && "" != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] =
          this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (null != this.daterangeUpdateTime && "" != this.daterangeUpdateTime) {
        this.queryParams.params["beginUpdateTime"] =
          this.daterangeUpdateTime[0];
        this.queryParams.params["endUpdateTime"] = this.daterangeUpdateTime[1];
      }
      listLeaderboard(this.queryParams).then((response) => {
        this.leaderboardList = response.data;
        // this.total = response.total;
        this.loading = false;
      });
    },
    endDate() {
      console.log(this.form.startTime);
      this.form.endTime = this.time[1];
      this.form.startTime = this.time[0];
    },
    //跳转到详情
    handleXq(e) {
      let tableName = "we";
      this.$tab.openPage(
        "查看[" + e.leaderboardName + "]盒子所有商品",
        "/top/xq/index/" + e.id
      );
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.openSettle = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodId: null,
        effectiveTime: null,
        leaderType: null,
        whosName: null,
        createTime: null,
        updateTime: null,
        leaderboardName: null,
      };
      
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeEffectiveTime = [];
      this.daterangeCreateTime = [];
      this.daterangeUpdateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加排行榜";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      // getLeaderboard(id).then((response) => {
      this.form = row;
this.time = [this.form.startTime, this.form.endTime];
      this.open = true;
      this.title = "修改排行榜";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            //          "id": "1",
            // "leaderboardName": "空车排行榜",
            // "ifOpen": 1,
            // "startTime": "2022-06-24 17:17:09",
            // "endTime": "2022-06-29 17:17:09"
            updateLeaderboard(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.whosName = this.$store.getters.name;
            addLeaderboard(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除排行榜编号为"' + ids + '"的数据项？')
        .then(function () {
          return delLeaderboard(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    jie_end() {
      console.log(this.form_end);
      settleLeaderboard(this.form_end).then((res) => {
        this.$modal.msgSuccess(res.msg);
        this.openSettle = false;
        this.getList();
      });

      //  this.$modal
      //   .confirm('是否确认结算"' + this.form_end.title + '"排行榜的数据项？')
      //   .then(function () {
      //     console.log(this.form_end);
      //     return settleLeaderboard(this.form_end);
      //   })
      //   .then(() => {
      //     this.getList();
      //     // this.$modal.msgSuccess("结算成功");
      //   })
      //   .catch(() => {});
    },
    //结算排行榜
    handleSettle(row) {
      this.openSettle = true;
      const ids = row.id || this.ids;
      this.form_end.type = row.id;
      this.form_end.isDisable = 1;
      this.form_end.endTime = row.endTime;
      this.form_end.startTime = row.startTime;
      this.form_end.title = row.leaderboardName;
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/leaderboard/export",
        {
          ...this.queryParams,
        },
        `leaderboard_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
